package com.desmart.ht;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.desmart.ht.calendarview.Calendar;
import com.desmart.ht.calendarview.CalendarLayout;
import com.desmart.ht.calendarview.CalendarView;
import com.desmart.ht.utils.WeekHelper;
import com.desmart.ht.view.SetPopView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivAdd;
    private CalendarView calendarView;
    private TextView tvDay;
    private TextView tvWeek;
    private TextView tvYearMonth;
    private List<String> mList = new ArrayList<>();
    private RecyclerView recyclerView;
    private TextView tvMonthSwitch;
    private TextView tvWeekSwitch;
    private CalendarLayout calendarLayout;
    private View tvDaySwitch;
    private TextView tvSelectDate;
    private TextView tvSelectDay;
    private MoNiAdapter adapter;
    private View headView;
    private View switchView;
    private View rlTopSwitch;
    private TextView tvScheduleSwitch;
    private LinearLayout llScheduleSwitch;
    private TextView tvToday;
    private ImageView ivSetting;
    private View ivEyes;
    private LinearLayout llSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initDatas();
        initCalenderViewListener();
        int top = recyclerView.getTop();
    }

    private void initDatas() {
        for (int i=0; i < 8; i++){
            mList.add("Moni");
        }
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MoNiAdapter(mList);
        headView = LayoutInflater.from(this).inflate(R.layout.list_item_head_main, null);
        tvSelectDate = headView.findViewById(R.id.tv_select_date);
        tvSelectDay = headView.findViewById(R.id.tv_select_day);
        adapter.addHeaderView(headView);
        recyclerView.setAdapter(adapter);

    }

    private void initViews() {
        ivAdd = findViewById(R.id.iv_add);
        ivAdd.setOnClickListener(this);
        calendarView = findViewById(R.id.calendarView);
        tvDay = findViewById(R.id.tv_day);
        tvWeek = findViewById(R.id.tv_week);
        llSwitch = findViewById(R.id.ll_switch);
        tvYearMonth = findViewById(R.id.tv_year_month);
        tvMonthSwitch = findViewById(R.id.tv_month_switch);
        tvWeekSwitch = findViewById(R.id.tv_week_switch);
        tvDaySwitch = findViewById(R.id.tv_day_switch);
        switchView = findViewById(R.id.layout_switch);
        rlTopSwitch = findViewById(R.id.rl_top_switch);
        calendarLayout = findViewById(R.id.calendarLayout);
        tvScheduleSwitch = findViewById(R.id.tv_schedule_switch);
        llScheduleSwitch = findViewById(R.id.ll_schedule_switch);
        ivSetting = findViewById(R.id.common_iv_setting);
        ivEyes = findViewById(R.id.common_iv_eyes);
        ivEyes.setOnClickListener(this);
        ivSetting.setOnClickListener(this);
        tvToday = findViewById(R.id.tv_today);
        tvToday.setOnClickListener(this);
        tvMonthSwitch.setOnClickListener(this);
        tvWeekSwitch.setOnClickListener(this);
        tvDaySwitch.setOnClickListener(this);
        tvScheduleSwitch.setOnClickListener(this);
        calendarView.setMonthViewScrollable(true);
        rlTopSwitch.setOnClickListener(this);
        switchView.setVisibility(View.GONE);
    }

    private void initCalenderViewListener() {
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                int year = calendar.getYear();
                int month = calendar.getMonth();
                int day = calendar.getDay();
                tvDay.setText(day + "日");
                tvYearMonth.setText(year + "年" + month + "日");
                tvSelectDate.setText(year + "-" + month + "-" + day);
                tvWeek.setText(WeekHelper.trammet(calendar.getWeek()));
                tvSelectDay.setText(String.valueOf(day));
                boolean currentDay = calendar.isCurrentDay();
                tvToday.setVisibility(currentDay ? View.GONE : View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View view) {
        llScheduleSwitch.setVisibility(View.GONE);
        if (view == ivAdd){
            NewScheduleActivity.start(this);
        }else if (view == tvMonthSwitch){
            calendarLayout.setVisibility(View.VISIBLE);
            calendarLayout.setOnlyMonthShow();
            calendarLayout.expand();
        }else if (view == tvWeekSwitch){
            calendarLayout.setVisibility(View.VISIBLE);
            calendarLayout.setOnlyWeekShow();
            calendarLayout.shrink();
        }else if (view == tvDaySwitch){
            calendarLayout.setVisibility(View.VISIBLE);
            calendarLayout.setOnlyWeekShow();
            calendarLayout.shrink();
        }else if (view == rlTopSwitch){
            if (switchView.getVisibility() == View.VISIBLE){
                calendarLayout.setSwitchClose();
                switchView.setVisibility(View.GONE);
            }else{
                calendarLayout.setSwitchOpen();
                switchView.setVisibility(View.VISIBLE);
            }
        }else if (view == tvScheduleSwitch){
            calendarLayout.setVisibility(View.GONE);
            llScheduleSwitch.setVisibility(View.VISIBLE);
        }else if (view == tvToday){
            calendarView.scrollToCurrent();
        }else if (view == ivSetting){
            setPopViewShow();
        }else if (view == ivEyes){
            EyeActivity.start(this);
        }


    }

    private void setPopViewShow() {
        SetPopView setPopView = new SetPopView(this);
        setPopView.show(ivSetting);
    }

}
